package com.zmzncs.lmtc.module.park.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zmzncs.lmtc.common.pojo.enums.OrderPayTypeEnum;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.reserve.ReserveStatusEnum;
import com.zmzncs.lmtc.module.order.dao.OrderInfoDao;
import com.zmzncs.lmtc.module.order.pojo.order_Info.OrderInfo;
import com.zmzncs.lmtc.module.park.pojo.reserve_record.ReserveRecord;
import com.zmzncs.lmtc.module.park.dao.ReserveRecordDao;
import com.zmzncs.lmtc.module.park.pojo.reserve_record.ReserveRecordDTO;
import com.zmzncs.lmtc.module.park.pojo.reserve_record.ReserveRecordInfoVO;
import com.zmzncs.lmtc.module.park.pojo.reserve_record.ReserveRecordManageForm;
import com.zmzncs.lmtc.module.park.service.ReserveRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 预约记录表 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2020-12-15
 */
@Service
public class ReserveRecordServiceImpl extends ServiceImpl<ReserveRecordDao, ReserveRecord> implements ReserveRecordService {

    @Autowired
    ReserveRecordDao reserveRecordDao;
    @Autowired
    OrderInfoDao orderInfoDao;


    @Override
    public IPage<ReserveRecordDTO> reserveRecordPage(ReserveRecordManageForm form) {

        IPage<ReserveRecordDTO> iPage = reserveRecordDao.reserveRecordPage(form.getPage(),form);
        List<ReserveRecordDTO> records = iPage.getRecords();
        if (records != null && !records.isEmpty()){
            for (ReserveRecordDTO dto : records) {
                String reserveStatus = ReserveStatusEnum.getMessage(dto.getStatus());
                dto.setReserveStatus(reserveStatus);
            }
        }

        return iPage;
    }

    @Override
    public ReserveRecordInfoVO getReserveRecordById(Long id) {

        ReserveRecordInfoVO reserveRecordInfoVO = new ReserveRecordInfoVO();

        /**预约记录详情 */
        ReserveRecordDTO reserveRecordDTO = reserveRecordDao.getReserveRecordById(id);

        Long orderId = reserveRecordDTO.getOrderId();
        if (orderId != null){
            OrderInfo orderInfo = orderInfoDao.selectById(orderId);
            String message = OrderPayTypeEnum.getMessage(orderInfo.getPayType());
            reserveRecordInfoVO.setCardType(message);
            reserveRecordInfoVO.setInTime(orderInfo.getInTime());
            reserveRecordInfoVO.setOutTime(orderInfo.getOutTime());
        }

        BeanUtils.copyProperties(reserveRecordDTO, reserveRecordInfoVO);
        reserveRecordInfoVO.setSubmitTime(reserveRecordDTO.getCreateTime());
        return reserveRecordInfoVO;
    }
}
